<template>
  <div class="app-container">
    <!--    查询对话框-->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="580px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="14">
          <el-form-item label-width="125px" label="销售单号" prop="somDjbh">
            <el-input
              v-model="queryParams.somDjbh"
              placeholder="请输入销售单号"
              clearable
              style="width: 180px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="订单状态" prop="somZtbz">
            <el-select
              v-model="queryParams.somZtbz"
              style="width: 142px"
              placeholder="请选择订单状态"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in somZtbzOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label-width="125px" prop="somPmgg">
            <span slot="label">
              <el-tooltip
                content="综合查询包含物料代码，品名规格，客户编码查询"
                placement="top"
              >
                <i class="el-icon-question"></i>
              </el-tooltip>
              综合查询
            </span>
            <el-input
              v-model="queryParams.somPmgg"
              clearable
              size="small"
              style="width: 400px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label-width="125px" label="建立时间" prop="dateRange">
            <el-date-picker
              clearable
              size="small"
              v-model="dateRange"
              :clearable="false"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="multiple"
          @click="handleCheck"
          v-hasPermi="['marketing:somCheck:check']"
        >
          审核
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['marketing:somCheck:remove']">-->
      <!--          删除-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['marketing:somCheck:export']"
        >
          导出
        </el-button>
      </el-col>
      <Tag type="dot" color="#3399ff">审核</Tag>
      <Tag type="dot" color="#FF44AA">结案待审批</Tag>
      <Tag type="dot" color="#bb0331">结案</Tag>
      <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      highlight-current-row
      v-loading="loading"
      :data="somList"
      :height="tableH"
      border
      stripe
      :cell-style="iCellStyle"
      ref="somCheckTable"
      @row-click="clickRow"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="销售单号"
        :show-overflow-tooltip="true"
        width="130"
        align="center"
        prop="somDjbh"
      />
      <el-table-column
        label="订单类型"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="somType"
      >
        <template #default="scope">
          <dict-tag :options="somAllTypeOptions" :value="scope.row.somType" />
        </template>
      </el-table-column>
      <el-table-column
        label="客户"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="somKhdm"
      >
        <template #default="scope">
          <dict-tag :options="customerOptions" :value="scope.row.somKhdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="客户编码"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="somCustwldm"
      />
      <el-table-column
        label="本厂物料"
        :show-overflow-tooltip="true"
        width="130"
        align="center"
        prop="somWldm"
      />
      <el-table-column
        label="品名规格"
        :show-overflow-tooltip="true"
        width="280"
        align="center"
        prop="somPmgg"
      />
      <el-table-column
        label="订单数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="somDdsl"
      />
      <el-table-column
        label="出货数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="somChsl"
      />
      <el-table-column
        label="下单日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="somXdrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.somXdrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="交货日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="somJhrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.somJhrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="计划导入"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="somPlanFlag"
      />
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="remark"
      />
      <el-table-column
        label="订单状态"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="somZtbz"
      >
        <template #default="scope">
          <dict-tag :options="somZtbzOptions" :value="scope.row.somZtbz" />
        </template>
      </el-table-column>
      <el-table-column
        label="建立人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="createBy"
      />
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        width="180"
        align="center"
        prop="createTime"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="somShry"
      />
      <el-table-column
        label="审核日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="somShrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.somShrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { listSom, delSom, exportSom, updateSomZtbz } from "@/api/marketing/som";

import { getAllCustomer } from "@/api/project/customer";

export default {
  name: "SomCheck",
  data() {
    return {
      // 遮罩层
      loading: true,
      tableH: 1,
      title: "",
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      somDjbhs: [],
      currentData: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 营销订单维护表格数据
      somList: [],
      // 是否显示弹出层
      open: false,
      openSearch: false,
      // 状态标志字典
      somZtbzOptions: [],
      //根据客户代码查询出物料信息
      selectedItmMstr: [],
      // 订单类型字典
      somTypeOptions: [],
      somAllTypeOptions: [],

      //客户类型
      customerOptions: [],

      // 日期范围
      dateRange: [
        this.$moment().startOf("month").toDate(),
        this.$moment().endOf("month").toDate(),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 18,
        somDjbh: null,
        somZtbz: "1",
        somPmgg: null,
        somXdrq: null,
        somXdrqEnd: null,
      },
      // 表单参数
      form: {},
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_order_ztbz").then((response) => {
      for (let i of response.data) {
        if (i.dictValue === "2") {
          i.dictLabel = "审核";
        }
      }

      this.somZtbzOptions = response.data;
    });
    this.getDicts("sys_order_style").then((response) => {
      this.somTypeOptions = response.data;
    });

    this.getAllDicts("sys_order_style").then((response) => {
      this.somAllTypeOptions = response.data;
    });

    getAllCustomer().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusCode + "";
        i.listClass = "default";
      }

      this.customerOptions = response.data;
    });
  },
  methods: {
    iCellStyle({ row, column, rowIndex, columnIndex }) {
      // 通过状态
      if (row.somZtbz == "2") {
        return "color:#3399ff";
      }
      // 结案申请状态
      else if (row.somZtbz == "3") {
        return "color:#FF44AA";
      }
      // 结案状态
      else if (row.somZtbz == "4") {
        return "color:#bb0331";
      }
    },
    /** 查询营销订单维护列表 */
    getList() {
      this.tableH = document.body.clientHeight - 280;
      if (this.dateRange !== null) {
        this.queryParams.somXdrq = this.moment(this.dateRange[0]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
        this.queryParams.somXdrqEnd = this.moment(this.dateRange[1]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      this.loading = true;
      listSom(this.queryParams).then((response) => {
        this.somList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.openSearch = false;
    },
    // 表单重置
    reset() {
      this.form = {
        somId: null,
        somDjbh: null,
        somCustwldm: null,
        somWldm: null,
        somDdsl: null,
        somKhdm: null,
        somXdrq: new Date(),
        somJhrq: new Date(),
        somKykc: null,
        remark: null,
        somZtbz: null,
        createBy: null,
        createTime: null,
        somShry: null,
        somShrq: null,
        somJary: null,
        somJarq: null,
        somType: null,
        somChsl: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    clickRow(row) {
      this.$refs.somCheckTable.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.somId);
      this.somDjbhs = selection.map((item) => item.somDjbh);
      this.currentData = selection;
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleSearch() {
      this.reset();
      this.title = "查询营销订单审核";
      this.openSearch = true;
    },
    /** 审核按钮操作 */
    handleCheck() {
      //已经审核的单据不能作废
      for (let i of this.currentData) {
        if (i.somZtbz === 2) {
          this.msgError("营销订单: [" + i.somDjbh + "] 已审核~");
          return;
        } else if (i.somZtbz === 3) {
          this.msgError(
            "营销订单: [" + i.somDjbh + "] 正在结案申请中，无法审核~"
          );
          return;
        } else if (i.somZtbz === 4) {
          this.msgError("营销订单: [" + i.somDjbh + "] 已结案，无法审核~");
          return;
        }
      }
      const somIds = this.ids;
      const mustZtbz = 1;
      const newZtbz = 2;
      this.$confirm("确认审核选中的单据？", "审核", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return updateSomZtbz(somIds, mustZtbz, newZtbz);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("审核成功!");
        })
        .catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete() {
      const somIds = this.ids;
      const somDjbh = this.somDjbhs;
      this.$confirm("确认删除销售单号[" + somDjbh + "]?删除不可恢复", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delSom(somIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有营销订单维护数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportSom(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped></style>
